package com.hmdp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmdp.dto.Result;
import com.hmdp.dto.ScrollResult;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Blog;
import com.hmdp.entity.User;
import com.hmdp.service.IBlogService;
import com.hmdp.service.IUserService;
import com.hmdp.utils.SystemConstants;
import com.hmdp.utils.UserHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/blog")
@Api(tags = "博客相关接口")
public class BlogController {

    @Resource
    private IBlogService blogService;


    @PostMapping
    @ApiOperation("保存博客")
    public Result saveBlog(@ApiParam("博客信息") @Valid @RequestBody Blog blog) {
        Long blogId = blogService.savBlog(blog);
        // 返回id
        return blogId != -1L ? Result.ok(blogId) : Result.fail("发布失败");
    }

    @PutMapping("/like/{id}")
    @ApiOperation("点赞博客")
    public Result likeBlog(@ApiParam("博客ID") @PathVariable("id") Long id) {
        // 修改点赞数量
        blogService.likeBlog(id);
        return Result.ok();
    }

    /**
     * 点赞排行榜(前几名)
     *
     * @param id
     * @return
     */
    @GetMapping("/likes/{id}")
    @ApiOperation("获取博客点赞排行榜")
    public Result querylikeBlog(@PathVariable("id") Long id) {
        List<UserDTO> userDTOS = blogService.querylikeBlog(id);
        return Result.ok(userDTOS);
    }


    @GetMapping("/of/me")
    @ApiOperation("查询当前用户博客")
    public Result queryMyBlog(@ApiParam("页码") @RequestParam(value = "current", defaultValue = "1") Integer current) {
        // 获取登录用户
        UserDTO user = UserHolder.getUser();
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", user.getId()).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @GetMapping("/hot")
    @ApiOperation("查询热门博客")
    public Result queryHotBlog(@ApiParam("页码") @RequestParam(value = "current", defaultValue = "1") Integer current) {
        return Result.ok(blogService.queryHotBlog(current));
    }

    @ApiOperation("查询博客详情")
    @GetMapping("/{id}")
    public Result queryBlogById(@ApiParam("博客ID") @PathVariable("id") Long id) {
        Blog blog = blogService.queryBlogById(id);
        return Result.ok(blog);
    }

    @ApiOperation("查询用户所发布的博客")
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    @ApiOperation("查询用户关注用户所发布的博客")
    @GetMapping("/of/follow")
    public Result queryBlogOfFollow(
            @RequestParam("lastId") Long max,
            @RequestParam(value = "offset", defaultValue = "0") Integer offset) {

        ScrollResult sr = blogService.queryBlogOfFollow(max, offset);
        return Result.ok(sr);
    }

}
